using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif

[System.Serializable]
public class InteractiveInfo : ScriptableObject
{
	[Header("Interactive")]
	public string itemName;
	public GameObject prefab;
	public Sprite image;
	public bool hidden;
	public Color mainColor;

	// Used by editor
	public bool folded;
	
	public virtual string[] GetStrings()
	{
		return new string[] { };
	}
	
	public virtual InteractiveInfo Clone()
	{
		return CloneInteractive<InteractiveInfo>();
	}
	
	public virtual T CloneInteractive<T>() where T : InteractiveInfo
	{
		T info = ScriptableObject.CreateInstance<T>();
		info.prefab = prefab;
		info.itemName = itemName;
		info.image = image;
		info.hidden = hidden;
		info.mainColor = mainColor;
		
		if (prefab != null)
		{
			info.name = prefab.name;
		}
		
		return info;
	}

	#region Editor
	public enum DrawResult
	{
		None,
		DontDisplay,
		Remove,
	}

	public static void DrawInteractives(List<InteractiveInfo> _interactives)
	{
		foreach (InteractiveInfo info in _interactives)
		{
			EditorGUILayout.BeginVertical("box");
			{
				DrawResult result = info.DrawEditor();
				if (result == DrawResult.Remove)
				{
					InteractiveManager.DestroyAsset(info);
					_interactives.Remove(info);
					return;
				}
			}
			EditorGUILayout.EndVertical();
		}
	}
	
	// Return true id it is to be removed
	public virtual DrawResult DrawEditor()
	{
		DrawResult result = folded ? DrawResult.DontDisplay : DrawResult.None;
		if (folded)
		{
			result = DrawHeaderFolded();
		}
		else
		{
			result = DrawHeaderFull();
		}

		return result;
	}
	
	private DrawResult DrawHeaderFull()
	{
		DrawResult result = folded ? DrawResult.DontDisplay : DrawResult.None;
		EditorGUILayout.BeginHorizontal();
		{
			EditorGUILayout.BeginVertical();
			{
				itemName = EditorGUILayout.TextField("Item Name", itemName);
				prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), true);
				image = (Sprite)EditorGUILayout.ObjectField("Image", image, typeof(Sprite), true);
				hidden = EditorGUILayout.Popup("Hidden", hidden ? 0 : 1, new string[] { "True", "False" }) == 0;
				mainColor = EditorGUILayout.ColorField("Main Color", mainColor);
			}
			EditorGUILayout.EndVertical();
			EditorGUILayout.BeginHorizontal(GUILayout.Width(75));
			{
				if (image)
				{
					GUILayout.Label(image.texture, GUILayout.Width(75), GUILayout.Height(75));
				}
				else
				{
					GUILayout.FlexibleSpace();
				}
				
				EditorGUILayout.BeginVertical();
				{
					result = DrawButtons();
				}
				EditorGUILayout.EndVertical();
			}
			EditorGUILayout.EndHorizontal();
		}
		EditorGUILayout.EndHorizontal();
		
		return result;
	}
	
	private DrawResult DrawHeaderFolded()
	{
		DrawResult result = folded ? DrawResult.DontDisplay : DrawResult.None;
		EditorGUILayout.BeginHorizontal();
		{
			EditorGUILayout.LabelField(itemName);
			if (DrawButtons() == DrawResult.Remove)
			{
				result = DrawResult.Remove;
			}
		}
		EditorGUILayout.EndHorizontal();
		
		return result;
	}

	private DrawResult DrawButtons()
	{
		string downArrow = ((char)9660).ToString();
		string leftArrow = ((char)9664).ToString();
		
		if (GUILayout.Button(folded ? leftArrow : downArrow, GUILayout.Width(20f)))
		{
			folded = !folded;
		}
		
		if (GUILayout.Button("X", GUILayout.Width(20f)))
		{
			bool result = EditorUtility.DisplayDialog("Destroy Asset",
			                                          "Are you sure you want to destroy the asset :\n" + this.itemName,
			                                          "Destroy", "Cancel");
			return result ? DrawResult.Remove : DrawResult.None;
		}
		
		if (GUILayout.Button("S", GUILayout.Width(20f)))
		{
			Selection.activeObject = this;
		}

		return DrawResult.None;
	}
	#endregion
}